package com.tongtong.middleware.blockchain.controller;

import com.tongtong.middleware.blockchain.client.bsn.BSNService;
import com.tongtong.middleware.blockchain.client.web3j.WEB3JService;
import com.tongtong.middleware.vo.response.AccountBalanceRes;
import com.tongtong.middleware.vo.response.bsn.PowerBalanceRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.xtt.common.response.ApiResult;

import java.io.IOException;
import java.math.BigInteger;

@RestController
@RequestMapping("/merge")
@Slf4j
public class MergeController {
    private static final String Manager = "0x3b5Be4c33249458534fcaa38Aa38B194E1d0EFC4";

    @Autowired
    WEB3JService web3jService;
    @Autowired
    BSNService bsnService;
	/**
	 * 查询账户余额
	 */
	@GetMapping("/account/balance")
	public ApiResult<AccountBalanceRes> mergeAccountBalance() {
		// 查询manager账户的能量值
        BigInteger chainBalance;
        double bsnBalance = 0;
        try {
            chainBalance = web3jService.getBalance(Manager);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 查询bsn账户算力值
        PowerBalanceRes res = bsnService.getPowerBalance();
        if (res.getErrorLogCode() == null) {
            bsnBalance = res.getData().getAccountBalance();
        }
        // 两个余额合并
        AccountBalanceRes accountBalanceRes = new AccountBalanceRes();
        accountBalanceRes.setChainBalance(chainBalance);
        accountBalanceRes.setBsnBalance(bsnBalance);
        return ApiResult.ok(accountBalanceRes);
    }
}
